﻿using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace Salient.Web.ApplicationServices
{
    ///<summary>
    ///</summary>
    public static class RoleServiceHelper
    {
        ///<summary>
        ///</summary>
        public const string EndpointName = "Role_JSON_AppService.axd";

        ///<summary>
        ///</summary>
        ///<param name="rootUrl"></param>
        ///<param name="cookies"></param>
        ///<returns></returns>
        public static string[] RolesGetRolesForCurrentUserAppSvc(string rootUrl, CookieContainer cookies)
        {
            string response = RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/GetRolesForCurrentUser", null, cookies)
                .GetResponse().GetResponseStream().Text();

            return new JavaScriptSerializer().CleanAndDeserialize<string[]>(response);
        }

        ///<summary>
        ///</summary>
        ///<param name="rootUrl"></param>
        ///<param name="cookies"></param>
        ///<param name="role"></param>
        ///<returns></returns>
        public static bool RolesIsCurrentUserInRoleAppSvc(string rootUrl, CookieContainer cookies, string role)
        {
            string response = RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/IsCurrentUserInRole", new
                {
                    role
                }, cookies)
                .GetResponse().GetResponseStream().Text();

            return new JavaScriptSerializer().CleanAndDeserialize<bool>(response);
        }
    }
}
